local dataTable = {}
local in_campaign = false -- 是否出现了全资源解锁的活动
local infra_mode = "exp" -- 搓玉？
local infra_custom_drone = "trade" -- 非搓玉阶段无人机的用途

-- baiduAI access key
dataTable.API = "yDWM7P174VwsFqKOuOwQkqsx"
dataTable.KEY = "KAHqOPpgOzmCMSBicrnonjrZYiTquBFI"

-- stage tag locations
dataTable.main_level_chapter = {3,8,9} -- encodes the last ep of each chapter
dataTable.main_level_table = {
    ["1-7"] = {x=835, y=335},
    ['1-11'] = {x=960, y=505},
    ["2-10"] = {x=940, y=670},
    ["6-11"] = {x=830, y=660},
    ["3-1"] = {x=835, y=525},
    ["3-2"] = {x=835, y=505},
    ["3-8"] = {x=835, y=330},
    ["3-3"] = {x=925, y=525},
    ["3-4"] = {x=930, y=655},
    ["5-10"] = {x=825, y=525},
    ["2-4"] = {x=835, y=510},
}

dataTable.chip_level_table = {
    ["芯片-XXI"] = {x=650, y=670},
    ["芯片-XXII"] = {x=1250, y=385},
}

dataTable.supply_level_table = {
    ["资源-XX本"] = {x=1420, y=300}
}

dataTable.annihilation_level = {
    ["剿灭-龙门市区"] = {x=1490, y=540}
}

dataTable.campaign_table = {
    -- 导吾先路
    current = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            GA = {x=1680, y=709}
        },
        game_table = {
            ["GA-8"] = {x=957, y=506},
        },
        need_scroll = {
            GA = true
        },
        exit_depth = 2,
        ui_override = {} 
    },
    
    -- 风雪过境
    fengxueguojing = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            BI = {x=1650, y=485}
        },
        game_table = {
            ["BI-6"] = {x=835, y=310},
            ["BI-8"] = {x=1395, y=590}
        },
        need_scroll = {
            BI = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 长夜临光
    nightnearl = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
          NL = {x=1680 ,y=650 }  
        },
        game_table = {
            ["NL-9"] = {x=835, y=360},
            ["NL-10"] = {x=830, y=748}
        },
        need_scroll = {
            NL = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 多索雷斯
    dousolios = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            DH={x=1420, y=500}
        },
        game_table = {
            ["DH-7"] = {x=560, y=477},
            ["DH-9"] = {x=1310, y=302}
        },
        need_scroll = {
            DH = true
        },
        exit_depth = 2,
        ui_override = {}
    },

    interlocking = {
        cpn_index = 1,
        prologue_delay = 1000,
        entrance_map = {
            FIN = {x=1600, y=650}
        },
        game_table = {
            ["FIN-TS"] = {x=970, y=445}
        },
        need_scroll = {
            FIN = false
        },
        exit_depth = 2,
        ui_override = {
            FIN = function()
                staging_start_button = {x=1700, y=960, c=0xe408a}
                staging_stage_focus_hint = {x=1110, y=940, c=0xde4545}
            end
        }
    },
    -- wolumonde
    wolumonde = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            TW = {x=1700, y=550 }
        },
        game_table = {
            ["TW-6"] = {x=585, y=690},
            ["TW-7"] = {x=972, y=545},
            ["TW-8"] = {x=1440, y=545}
        },
        need_scroll = {
            TW = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 覆潮之下
    fuchaozhixia = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            SV={x=1710, y=680}
        },
        game_table = {
            ["SV-7"] = {x=395, y=664},
            ["SV-8"] = {x=1045, y=680},
            ["SV-9"] = {x=1507, y=405}
        },
        need_scroll = {
            SV = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 遗尘漫步
    yichenmanbu = {
        cpn_index=1,
        prologue_delay=1500,
        entrance_map = {
            WD={x=1605, y=820}
        },
        game_table = {
            ["WD-6"] = {x=522, y=534},
            ["WD-7"] = {x=280, y=710},
            ["WD-8"] = {x=740, y=710}
        },
        need_scroll = {
            WD = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 生于黑夜
    shengyuheiye = {
        cpn_index = 6,
        prologue_delay = 1500,
        entrance_map = {
            DM = {x=1335, y=785}
        },
        game_table = {
            ["DM-7"] = {x=1400, y=560},
            ["DM-8"] = {x=919, y=475}
        },
        need_scroll = {
            DM = false
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 监狱风云
    jianyufengyun = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            MB = {x=1700, y=245}
        },
        game_table = {
            ["MB-8"] = {x=1700, y=785}
        },
        need_scroll = {
            MB = false
        },
        exit_depth = 2,
        ui_override = {}
    },
    r6 = {
        cpn_index = 7,
        prologue_delay = 1200,
        entrance_map = {
            OD = {x=167, y=467}
        },
        game_table = {},
        need_scroll = {
            OD = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    -- 画中人
    huazhongren = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            ["WR"] = {x=1740, y=780}
        },
        game_table = {
            ["WR-10"] = {x=835, y=580}
        },
        need_scroll = {
            WR = true
        },
        exit_depth = 2,
        ui_override = {}
    },
    maisuiyuzanmeishi = {
        cpn_index = 6,
        prologue_delay = 1500,
        entrance_map = {
            ['ex'] = {x=280, y=705}
        },
        game_table = {},
        need_scroll = false,
        exit_depth = 2,
        ui_override = {}
    },

    milinhanjiang = {
        cpn_index = 1,
        prologue_delay = 3000,
        entrance_map = {
            RI = { x=1750, y=500 }
        },
        game_table = {
            ["RI-6"] = {x=835, y=723},
            ["RI-7"] = {x=835, y=450},
            ["RI-8"] = {x=835, y=605}
        },
        need_scroll = {
            RI = true
        },
        ui_override = {}
    },

    huolanzhixin = {
        cpn_index = 6,
        prologue_delay = 2000,
        entrance_map = {
            ex1 = {x=1650, y=500},
            ex2 = {x=1600, y=635}
        },
        game_table = {},
        need_scroll = {
            ex1 = false,
            ex2 = true
        },
        ui_override = {
            ex1 = function () end,
            ex2 = function ()
                staging_start_button = {x=1700, y=960, c=0xc76fb0}
            end
        }
    }
}

dataTable.staging_plan = {
    ["WR-10"] = 999
    -- ["1-7"] = 999
}

-- 每天每种资源本排第几个 (Mon~Sun)
if in_campaign then 
    dataTable.supply_schedule = {
        ["经验"] = {5,5,5,5,5,5,5},
        ["红票"] = {3,1,1,3,1,2,2},
        ["技能"] = {1,3,3,1,3,1,3},
        ["碳"]   = {4,2,4,2,4,3,1},
        ["钱"]   = {2,4,2,4,2,4,4},
    }

    dataTable.chip_schedule = {
        ["重医"] = {1,3,3,1,1,4,1},
        ["先辅"] = {3,4,1,2,3,2,2},
        ["特近"] = {4,2,2,3,4,3,3},
        ["术狙"] = {2,1,4,4,2,1,4},
    }
else
    dataTable.supply_schedule = {
        ["经验"] = {5,5,5,5,5,5,5},
        ["红票"] = {3,0,0,3,0,2,2},
        ["技能"] = {0,3,3,0,3,0,3},
        ["碳"]   = {4,0,4,0,4,3,0},
        ["钱"]   = {0,4,0,4,0,4,4},
    }

    dataTable.chip_schedule = {
        ["重医"] = {1,0,0,1,1,0,1},
        ["术狙"] = {2,1,0,0,2,1,0},
        ["先辅"] = {0,0,1,2,0,2,2},
        ["特近"] = {0,2,2,0,0,3,3},
    }
end

-- 2nd generation of shift info (deprecated)
-- fails sometime due to uncertain template matching threshold
dataTable.nameBM = {
    ["12F"] = "23:49:07000fc00fff9f801ff807ffdfc01ffe03ffefe00e0f01e000700203c0e000380000e070001c00007038000e0000381c000700003c0e000380001c070001c0000e03ffc0e0000e01ffe070000f00fff03800070078001c00078038000e0007801c00070007800e0003800780070001c00780038000e0078001c00fff87ffe0e007ffc3fff07003ffe1fff8380@000"
}

-- aux to resolve name conflicts
dataTable.collideDict = {
    ["红"] = {"红云", "红豆"},
    ["空"] = {"空爆"},
    ["梅"] = {"梅尔"},
    ["黑"] = {"黑角"},
    ["阿"] = {"阿消","阿米娅"}
}

-- aux to fix detection flaw
dataTable.clashTable = {
    ["Lancet-2"]={"Lancet -2"},
    ["THRM-EX"]={"THRM-EⅩ"},
    ["初雪"]={"初"},
    ["卡缇"]={"卡堤"},
    ["崖心"]={"厘"},
    ["幽灵鲨"]={"幽灵盈", "幽灵"},
    ["斑点"]={"班点"},
    ["普罗旺斯"]={},
    ["暴行"]={"暴"},
    ["末药"]={"未药"},
    ["梓兰"]={"梓"},
    ["清流"]={"流"},
    ["白雪"]={"白"},
    ["白面鸮"]={"白面鹗"},
    ["芬"]={"分"},
    ["锡兰"]={"锡"},
    ["黑"]={"里"},
    ["铃兰"] = {"铃"}, 
    ["亚叶"] = {"卟", "叶"},
    ["孑"] = {"子","孓"},
    ["稀音"] = {"稀"},
    ["浊心斯卡蒂"] = {"虫心斯卡蒂"},   
}

-- 3rd generation of shift info
dataTable.priority = {
    trade = {
        "拉普兰德", "德克萨斯", "巫恋", "能天使", "古米", 
        "空爆", "月见夜", "夜烟", "慕斯", "安比尔", 
        "夜刀", "远山", "卡夫卡", "龙舌兰"
    },
    prod = {
        "断罪者", "红豆", "霜叶", "食铁兽", "白雪", 
        "Castle-3", "帕拉斯", "砾", "夜烟", "斑点", 
        "清流", "迷迭香", "槐琥", "梅尔", "远牙", 
        "灰毫", "野鬃", "白面鸮", "赫默", "水月",
        "罗比菈塔", "香草", "石棉", "夜刀", "刻俄柏", 
        "红云", "艾雅法拉", "地灵", "炎熔", "薄绿", 
        "月见夜"
    },
    meet = {
        "安洁莉娜", "极境", "微风", "苦艾", "莫斯提马",
        "梅", "塞雷亚", "山", "陈", "远山",
        "守林人", "杰克", "白雪"
    },
    office = {
        "艾雅法拉", "地灵", "伊桑", "山"
    },
    power = {
        "雷蛇", "炎狱炎熔", "格雷伊", "布丁", "清流", "炎熔"
    }
}

dataTable.checkTable = {
    -- following hits
    "分", "班点", "白", "梓", "里", "白面鹗", "Lancet -2", "暴", "THRM-EⅩ", "铃", "初", "锡", "卡堤", "未药", "幽灵盈", "子", "孓", "卟", "幽灵", "厘", "叶", "稀", "2F", "流", "虫心斯卡蒂", "心斯卡蒂"
}

--[[
    noraml : control office meeting
     - shift_idx
      > 全员轮岗制下，对应当前出勤号
     - on_duty
      > 这一班已经工作的岗数
    
    binded : ge power
     > 格式与上相同，但是多个房间以数组的形式联合存储，保证修改任何一个房间的时候可以访问到其他房间的状态

    pipeline ：gold exp
     - shift_idx
      > 个人轮换制下，对应下一个需要下的人
     - on_duty
      > 当前是本房间的第几岗， 3岗歇一次
]]

local shift_243 ={
    shift = {
        ["control"] = {
            {staff = {"陈","诗怀雅","杜宾","苇草","清道夫"}, max_duty=3},
            {staff = {"灰烬","霜华","闪击","战车","阿米娅"}, max_duty=99999}
        },
        ["meet"] = {
            {staff={"陈", "梅"}, max_duty=3},
            {staff={"苦艾", "远山"}, max_duty=3}
        },
        ["office"] = {
            {staff = {"艾雅法拉"}, max_duty=3},
            {staff = {"山"}, max_duty=3}
        },
        ["trade"] = {
            {staff={"巫恋", "柏喙", "卡夫卡"}, max_duty=3}, -- 摸金专用班
            -- {staff={"银灰", "孑", "桃金娘"}, max_duty=3},
            {staff={"德克萨斯","拉普兰德","能天使"}, max_duty=3},
            {staff={"月见夜","古米","空爆"}, max_duty=6},
        },
        ["gold"] = {staff={"夜烟","斑点","清流","砾"}, max_duty=3},
        ["exp"] = {staff={"断罪者","红豆","食铁兽","霜叶"}, max_duty=3},
        ["power"] = {
            {staff={"格雷伊"}, max_duty=3},
            {staff={"伊芙利特"}, max_duty=3},
            {staff={"雷蛇"}, max_duty=3},
            {staff={"炎狱炎熔"}, max_duty=3}
        },
        ["ge"] = {
            {staff={"刻俄柏","豆苗","红云"}, max_duty=6},
            {staff={"梅尔","赫默","白面鸮"}, max_duty=3},
            {staff={"调香师","杰西卡","香草"}, max_duty=3}
        }
    -- {"史都华德","泡普卡","槐琥"},{"艾雅法拉","地灵","炎熔"}
    },
    rooms = {
        {nroom=2, rname={"control", "meet"}, rtype={"normal", "normal"}, dorm_idx = 0},
        {nroom=5, rname={"trade1", "trade2", "power1"}, rtype={"binded", "binded", "binded"}, dorm_idx = 4},
        {nroom=5, rname={"gold", "exp", "power2", "_", "office"}, rtype = {"pipeline", "pipeline", "binded", "_", "normal"}, dorm_idx = 4},
        {nroom=5, rname={"ge1", "ge2", "power3"}, rtype = {"binded", "binded", "binded"}, dorm_idx = 4},
        {nroom=1, rname={}, rtype={}, dorm_idx = 1}
    },
    trade_index = 1,
    exp_index = 1
}

local shift_333 = {
    shift = {
    ["control"] = {
        {staff = {"陈","星熊","诗怀雅","雪雉","吽"}, max_duty=3},
        {staff = {"灰烬","霜华","闪击","战车","凯尔希"}, max_duty=99999}
    },
    ["meet"] = {
        {staff={"陈", "梅"}, max_duty=3},
        {staff={"苦艾", "远山"}, max_duty=3}
    },
    ["office"] = {
        {staff = {"宴"}, max_duty=3},
        {staff = {"伊桑"}, max_duty=3}
    },
    ["trade"] = {
        {staff={"巫恋", "柏喙", "卡夫卡"}, max_duty=3},
        {staff={"德克萨斯","拉普兰德","能天使"}, max_duty=3},
        {staff={"月见夜","古米","空爆"}, max_duty=6}, -- 值得注意的是，只要两个4岗贸易站之间只差一岗，7岗就永远不会影响换班
        {staff={"银灰", "孑", "史都华德"}, max_duty=3}
    },
    ["gold"] = {staff={"斑点","砾","清流","夜烟"}, max_duty=3},
    ["stone"] = {staff={"艾雅法拉","地灵","炎熔","香草"}, max_duty=3},
    ["power"] = {
        {staff={"格雷伊"}, max_duty=3},
        {staff={"伊芙利特"}, max_duty=3},
        {staff={"雷蛇"}, max_duty=3},
        {staff={"格劳克斯"}, max_duty=3}
    },
    ["ge"] = {
        {staff={"刻俄柏","豆苗","红云"}, max_duty=6},
        {staff={"梅尔","赫默","白面鸮"}, max_duty=3},
        -- 摸金专用
        {staff={"调香师","杰西卡","香草"}, max_duty=3}
    }
    -- {"史都华德","泡普卡","槐琥"},{"艾雅法拉","地灵","炎熔"}
    },
    rooms = {
        {nroom=2, rname={"control", "meet"}, rtype={"normal", "normal"}, dorm_idx = 0},
        {nroom=5, rname={"trade1", "trade2", "power1"}, rtype={"binded", "binded", "binded"}, dorm_idx = 4},
        -- -- 摸金配列
        {nroom=5, rname={"gold", "ge2", "power2", "_", "office"}, rtype = {"pipeline", "binded", "binded", "_", "normal"}, dorm_idx = 4}, -- 如果搓玉 ge2 -> stone;对应的binded -> pipeline
        {nroom=5, rname={"ge1", "trade3", "power3"}, rtype = {"binded", "binded", "binded"}, dorm_idx = 4}, -- 如果搓玉 ge1 -> ge; 对应的binded -> normal

        -- 搓玉配列
        -- {nroom=5, rname={"gold", "stone", "power2", "_", "office"}, rtype = {"pipeline", "pipeline", "binded", "_", "normal"}, dorm_idx = 4}, -- 如果搓玉 ge2 -> stone;对应的binded -> pipeline
        -- {nroom=5, rname={"ge", "trade3", "power3"}, rtype = {"normal", "binded", "binded"}, dorm_idx = 4}, -- 如果搓玉 ge1 -> ge; 对应的binded -> normal
        
        {nroom=1, rname={}, rtype={}, dorm_idx = 1}
    },
    trade_index = 1,
    exp_index = 1
}

local shift_153 = {
    shift = {
        ["control"] = {
            {staff = {"陈","诗怀雅","杜宾","苇草","清道夫"}, max_duty=3},
            {staff = {"灰烬","霜华","闪击","战车","阿米娅"}, max_duty=99999}
        },
        ["meet"] = {
            {staff={"陈", "梅"}, max_duty=3},
            {staff={"苦艾", "远山"}, max_duty=3}
        },
        ["office"] = {
            {staff = {"艾雅法拉"}, max_duty=3},
            {staff = {"山"}, max_duty=3}
        },
        ["trade"] = {
            {staff={"巫恋", "龙舌兰", "卡夫卡"}, max_duty=3}, -- 摸金专用班
            {staff={"德克萨斯","拉普兰德","能天使"}, max_duty=0},
            -- {staff={"月见夜","古米","空爆"}, max_duty=6},
        },
        ["gold"] = {staff={"夜烟","斑点","梅尔","砾"}, max_duty=3},
        ["exp"] = {staff={"霜叶","断罪者","红豆","食铁兽"}, max_duty=3},
        ["power"] = {
            {staff={"格雷伊"}, max_duty=3},
            {staff={"布丁"}, max_duty=3},
            {staff={"雷蛇"}, max_duty=3},
            {staff={"炎狱炎熔"}, max_duty=3}
        },
        ["ge"] = {
            {staff={"刻俄柏","帕拉斯","红云"}, max_duty=6},
            {staff={"灰毫","远牙","野鬃"}, max_duty=3},
            {staff={"水月","罗比菈塔","香草"}, max_duty=3},
            {staff={"白雪","Castle-3","槐琥"}, max_duty=3}
        }
    -- {"艾雅法拉","地灵","炎熔"}
    },
    rooms = {
        {nroom=2, rname={"control", "meet"}, rtype={"normal", "normal"}, dorm_idx = 0},
        {nroom=5, rname={"trade", "ge3", "power1"}, rtype={"normal", "binded", "binded"}, dorm_idx = 4},
        {nroom=5, rname={"gold", "exp", "power2", "_", "office"}, rtype = {"pipeline", "pipeline", "binded", "_", "normal"}, dorm_idx = 4},
        {nroom=5, rname={"ge1", "ge2", "power3"}, rtype = {"binded", "binded", "binded"}, dorm_idx = 4},
        {nroom=1, rname={}, rtype={}, dorm_idx = 1}
    },
    trade_index = 1, -- 1 号房间为贸易时在贸易界面中标签索引
    gold_index = 3  -- 4 号房间为制造，同上
}

dataTable.infra_mode = infra_mode
if infra_mode == "stone" then
    dataTable.shift = shift_333.shift
    dataTable.rooms = shift_333.rooms
    dataTable.drone_strategy = "gold"
elseif infra_mode == "gold" then
    dataTable.shift = shift_333.shift
    dataTable.rooms = shift_333.rooms
    dataTable.drone_strategy = infra_custom_drone
elseif infra_mode == "exp" then
    dataTable.shift = shift_153.shift
    dataTable.rooms = shift_153.rooms
    dataTable.drone_strategy = infra_custom_drone
end

local json = ts.json

dataTable.load_json = function (file)
    local fobj = io.open(file, "r")
    if fobj == nil then
        return {}
    end

    local jstr = fobj:read("*all")
    fobj:close()
    if jstr == nil then 
        return {}
    end

    local ret = json.decode(jstr)
    return ret
end

dataTable.dump_json = function(obj, file)
    local jstr = json.encode(obj)
    local f = io.open(file, "w")
    f:write(jstr)
    f:close()
end

-- towards pixel count free layout recording
local x, y = getScreenSize()
if x < y then 
    y, x = getScreenSize()
end
dataTable.ScrX = x
dataTable.ScrY = y

return dataTable
